package com.icat.house.web.controller.user;

import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.icat.house.common.CaptchaRender;
import com.icat.house.common.Constant;
import com.icat.house.common.DateUtil;
import com.icat.house.model.UserInfo;
import com.icat.house.service.market.SmsService;
import com.icat.house.web.controller.BaseController;
import com.icat.house.web.intercept.LoginIntercept;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;

@Before(LoginIntercept.class)
@ControllerBind(controllerKey = "/user")
public class UserController extends BaseController {
	/**
	 * 登录
	 */
	@Clear
	@ActionKey("/user/login")
	public void user_login() {
		String idCard = getPara("id_card");
		String password = getPara("password");
		String captcha = getPara("captcha");
		String r = getPara("r");
		JSONObject result = new JSONObject();
		if (StrKit.isBlank(captcha)) {
			result.put(Constant.ERROR, Constant.CAPTCHA_NULL);
			renderJson(result);
			return;
		}
		if (!CaptchaRender.validate(getSessionAttr(r), captcha)) {
			result.put(Constant.ERROR, Constant.CAPTCHA_ERROR);
			renderJson(result);
			return;
		}
		if (StrKit.isBlank(idCard)) {
			result.put(Constant.ERROR, Constant.IDCARD_NULL);
			renderJson(result);
			return;
		}
		if (StrKit.isBlank(password)) {
			result.put(Constant.ERROR, Constant.PASSWORD_NO);
			renderJson(result);
			return;
		}
		UserInfo user = UserInfo.dao.isUser(idCard, password);
		if (!StrKit.notNull(user)) {
			result.put(Constant.ERROR, Constant.ERROR_USER_PASSWOED_ERROR);
			renderJson(result);
			return;
		}
		user.set("token_validity", DateUtil.getDate(7));
		user.update();
		result.put(Constant.SUCCESS, user);
		renderJson(result);
	}

	/**
	 * 用户信息
	 */
	@ActionKey("/user/info")
	public void user_info() {
		JSONObject object = new JSONObject();
		UserInfo user = getAttr("user");
		object.put(Constant.SUCCESS, JSONObject.parseObject(JsonKit.toJson(user)));
		renderJson(object);
	}

	/**
	 * 手机短信验证码发送
	 */
	@Clear
	@ActionKey("/user/sendauthcode")
	public void sendAuthCode() {
		JSONObject jsonObject = new JSONObject();
		String phone = getPara("phone");
		if (StrKit.notBlank(phone)) {
			int r = (int) ((Math.random() * 9 + 1) * 100000);
			HttpSession session = getSession();
			session.setAttribute(phone, String.valueOf(r));
			session.setMaxInactiveInterval(600);
			boolean b = SmsService.sendAuthCode(phone, r);
			if (b) {
				jsonObject.put(Constant.SUCCESS, Constant.SUCCESS_SEND);
			} else {
				jsonObject.put(Constant.ERROR, Constant.ERRROR_INSTRUCT);
			}
		} else {
			jsonObject.put(Constant.ERROR, Constant.PHONE_NO);
		}
		renderJson(jsonObject);
	}

	/**
	 * 手机短信验证码发送
	 */
	@Clear
	@ActionKey("/user/sendsmsinfo")
	public void sendSmsInfo() {
		JSONObject jsonObject = new JSONObject();
		String phone = getPara("phone");
		if (StrKit.notBlank(phone)) {
			String name = "李鑫林";
			String time = "2017-11-07 19:00:00";
			boolean b = SmsService.sendInfo(phone, name, time);
			if (b) {
				jsonObject.put(Constant.SUCCESS, Constant.SUCCESS_SEND);
			} else {
				jsonObject.put(Constant.ERROR, Constant.ERRROR_INSTRUCT);
			}
		} else {
			jsonObject.put(Constant.ERROR, Constant.PHONE_NO);
		}
		renderJson(jsonObject);
	}

	@Clear
	@ActionKey("/user/update/password")
	public void updatePassword() {
		String phone = getPara("phone");
		String password = getPara("password");
		String id_card = getPara("id_card");
		String captcha = getPara("captcha");
		JSONObject result = new JSONObject();
		if (StrKit.isBlank(captcha)) {
			result.put(Constant.ERROR, Constant.CAPTCHA_NULL);
			renderJson(result);
			return;
		}
		if (getSessionAttr(phone) == null) {
			result.put(Constant.ERROR, Constant.CAPTCHA_NULL);
			renderJson(result);
			return;
		}
		if (!getSessionAttr(phone).equals(captcha)) {
			result.put(Constant.ERROR, Constant.CAPTCHA_ERROR);
			renderJson(result);
			return;
		}
		UserInfo info = UserInfo.dao.getUserInfo(id_card, phone);
		if (!StrKit.notNull(info)) {
			result.put(Constant.ERROR, Constant.PHONE_NO_EXIS);
			renderJson(result);
			return;
		}
		boolean b = info.set("password", HashKit.md5(password)).update();
		if (!b) {
			result.put(Constant.ERROR, Constant.ERROR_MESSAGE_UPDATE);
			renderJson(result);
			return;
		}
		result.put(Constant.SUCCESS, Constant.SUCCESS_MESSAGE_UPDATE);
		renderJson(result);
	}

}